﻿Imports OMANE.Common.UI
Imports System.Data

Public Class ViewTester

End Class

Public Class VMTester
    Inherits ViewModelBase
    Implements IViewModelTabSection



    Dim mdl As ViewModelModeleInterne
    Dim ps As ViewModelParametres
    Dim ismdl As Boolean = False
    Dim param As ModelParamType

    Dim data As DataSetAdapter
    Dim paramsdata As Collections.IEnumerable

    Sub New()

    End Sub

    Sub New(ByVal mdl As ViewModelModeleInterne, ByVal ps As ViewModelParametres, ByVal ismdl As Boolean)
        Me.mdl = mdl
        Me.ps = ps
        Me.ismdl = ismdl
        'ResetExecute(Nothing)
    End Sub

    Private _ResetCommand As New DelegateCommand(Of Object)(AddressOf ResetExecute)
    Public ReadOnly Property ResetCommand As DelegateCommand(Of Object)
        Get
            Return _ResetCommand
        End Get
    End Property
    Private Sub ResetExecute(ByVal parameter As Object)

        Dim t As New DataTable("maintable")
        Dim idcol = t.Columns.Add("_ID_", GetType(Integer))
        idcol.AllowDBNull = False
        idcol.AutoIncrement = True
        idcol.Unique = True
        t.PrimaryKey = New DataColumn() {idcol}
        t.Rows.Add(t.NewRow())

        For Each c In mdl.Model.DefaultEntity.Properties
            Select Case c.Type
                Case ModelParamUnderLyingType.Boolean
                    t.Columns.Add(c.Name, GetType(Boolean))
                Case ModelParamUnderLyingType.Case
                    t.Columns.Add(c.Name, GetType(String))
                Case ModelParamUnderLyingType.Double
                    t.Columns.Add(c.Name, GetType(Double))
                Case ModelParamUnderLyingType.Date
                    t.Columns.Add(c.Name, GetType(Date))
                Case ModelParamUnderLyingType.Time
                    t.Columns.Add(c.Name, GetType(TimeSpan))
                Case ModelParamUnderLyingType.Mixed
                    t.Columns.Add(c.Name, GetType(Object))
            End Select
        Next

        Dim pars = From p In ps.Params Select p.Name

        data = New DataSetAdapter(t, pars.ToArray())

        paramsdata = From p In ps.Params
                     Select New VMTester(mdl, ps, False) With {.data = Me.data, .param = p}


        OnPropertyChanged("Table")
        OnPropertyChanged("Params")
    End Sub

    Private _AddColCommand As New DelegateCommand(Of Object)(AddressOf AddColExecute)
    Public ReadOnly Property AddColCommand As DelegateCommand(Of Object)
        Get
            Return _AddColCommand
        End Get
    End Property
    Private Sub AddColExecute(ByVal parameter As Object)
        SelectedParam.data.AddCol(SelectedParam.param.Name, parameter.Name)
        OnPropertyChanged("Table")
        SelectedParam.OnPropertyChanged("Table")
    End Sub
    Private _RemoveColCommand As New DelegateCommand(Of Object)(AddressOf RemoveColExecute)
    Public ReadOnly Property RemoveColCommand As DelegateCommand(Of Object)
        Get
            Return _RemoveColCommand
        End Get
    End Property
    Private Sub RemoveColExecute(ByVal parameter As Object)

    End Sub

    Private _ComputeCommand As New DelegateCommand(Of Object)(AddressOf ComputeExecute)
    Public ReadOnly Property ComputeCommand As DelegateCommand(Of Object)
        Get
            Return _ComputeCommand
        End Get
    End Property
    Private Sub ComputeExecute(ByVal parameter As Object)
        Dim fi = GetType(LocalExpression).GetField("GetString", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)


        Dim a = data.columns(1) + 1


        Dim b = LocalExpression.Choose( _
            data.parameters(0) < data.parameters(1), _
            data.parameters(1) / data.parameters(0), _
            data.parameters(0) / data.parameters(1))


        Dim c = (data.columns(1) * data.parameters(2) + data.parameters(1)) / data.parameters(0)

        Dim col = data.main.table.Columns.Add()

        Dim str As String = CType(fi.GetValue(c), Func(Of String)).Invoke()

        col.Expression = str
    End Sub




    Public Property Icone As String Implements Common.UI.IViewModelTabSection.Icone

    Public Property Title As String Implements Common.UI.IViewModelTabSection.Title
        Get
            If ismdl Then
                Return "Table Principale"
            Else
                Return "Table de " & param.Name
            End If
        End Get
        Set(ByVal value As String)

        End Set
    End Property

    Public ReadOnly Property ModelColumns As IEnumerable
        Get
            Return mdl.Model.DefaultEntity.Properties
        End Get
    End Property

    Public ReadOnly Property Table As DataView
        Get
            If data Is Nothing Then Return Nothing
            If ismdl Then
                Return data.data.Tables("maintable").DefaultView
            Else
                If data.data.Tables(param.Name) Is Nothing Then Return Nothing
                Return data.data.Tables(param.Name).DefaultView
            End If
        End Get
    End Property

    Public ReadOnly Property Params As IEnumerable
        Get
            Return Me.paramsdata
        End Get
    End Property

    Public Property SelectedParam As VMTester


    Public Property IsClosable As Boolean Implements IViewModelTabSection.IsClosable
End Class
